package cn.wolfcode.geomagicstudio.builder;


import cn.wolfcode.geomagicstudio.read.PropertiesRead;

import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class IServiceGeomagicStudio {

    private static Map<String,String> propertiesMap;

    static {
        PropertiesRead propertiesRead = new PropertiesRead();
        try {
            propertiesMap = propertiesRead.Read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public void ControllerBuild() throws Exception {

        String Url = propertiesMap.get("%packageUrl%").replace(".","/");



        URL url = Thread.currentThread().getContextClassLoader().getResource("generator.properties");
        String Outreplace = url.getFile().replace("target/classes/generator.properties", "/src/main/java/"+Url+"/service/I")+propertiesMap.get("%Class%")+"Service.java";
        String Inreplace = url.getFile().replace("generator.properties", "generator/model/IServiceModel.text");



        InputStreamReader reader = new InputStreamReader(new FileInputStream(Inreplace));
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(Outreplace));
        BufferedReader bufferedReader = new BufferedReader(reader);
        BufferedWriter bufferedWriter = new BufferedWriter(writer);

        char[] line =new char[2046];

        StringBuffer stringBuffer = new StringBuffer();
        List<String> strings = bufferedReader.lines().map(x -> machining(x)).collect(Collectors.toList());

        for (String string : strings) {
            bufferedWriter.write(string+"\n");
        }
//
//        int i =0;
//         while ( bufferedReader.read(line,i,2046)>=0){
//
//
//                 String s = new String(line);
//                 System.out.println(machining(s));
//                 stringBuffer.append(machining(s));
//                 line=new char[2046];
//
//
//         }
//
//      bufferedWriter.write(stringBuffer.toString());
//

        bufferedReader.close();
        bufferedWriter.close();
        reader.close();
         writer.close();


    }



    public String machining(String in){
        String s = in.replaceAll("%Object%",propertiesMap.get("%Object%"));
        String s1 = s.replaceAll("%Class%",propertiesMap.get("%Class%"));
        String s2 = s1.replaceAll("%packageUrl%",propertiesMap.get("%packageUrl%"));
        String s3 = s2.replaceAll("%Model%",propertiesMap.get("%Model%"));


        return s3;
    }


}
